package com.dsmusic.musicpro.controller;

import com.dsmusic.musicpro.dao.UserDaoImpl;
import com.dsmusic.musicpro.pojo.Detail;
import com.dsmusic.musicpro.pojo.Lyric;
import com.dsmusic.musicpro.pojo.Music;
import com.dsmusic.musicpro.service.MusicService;
import com.dsmusic.musicpro.service.MusicServiceImpl;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Xjk
 */

@Controller
public class HomepageController {

    @RequestMapping(value = {"/search"},method = RequestMethod.GET)
    public String getIndex(){
        return "search.html";
    }

    @ResponseBody
    @RequestMapping(value = "/musicSearch2")
    public List<Music> getMusicPage(@RequestParam("key") String key,@RequestParam("page") String page){
        System.out.println(key);
        System.out.println(page);
        MusicService musicService = new MusicServiceImpl();
        List<Music> musics = musicService.searchMusic(key, page);
        return musics;
    }
    @RequestMapping(value = "/musicSearch")
    public String getMusicPage2(@RequestParam("key") String key, Model model){
        System.out.println(key);
        model.addAttribute("name",key);
        MusicService musicService = new MusicServiceImpl();
        List<Music> musics = musicService.searchMusic(key, "1");
        model.addAttribute("musics",musics);
        System.out.println(musics);

        return "music.html";
    }


    @RequestMapping("/Details")
    public String getDetails(@RequestParam("rid") String rid,Model model){
        System.out.println(rid);
        MusicService service = new MusicServiceImpl();
        Detail detail = service.getDetail(rid);
        List<Lyric> lyrics = service.getLyric(rid);
//        System.out.println(detail);
//        System.out.println(lyrics);
//        for (Lyric lyric : lyrics) {
//            System.out.println(lyric.getTime());
//            System.out.println(lyric.getLineLyric());
//        }
        model.addAttribute("detail",detail);
        model.addAttribute("lyrics",lyrics);
        List<Double> timelist = new ArrayList<>();
        for (Lyric lyric : lyrics) {
            timelist.add(Double.valueOf(lyric.getTime()));
        }
        model.addAttribute("timeList",timelist);
        return "details.html";
    }
    @RequestMapping("/register0")
    public String register(@RequestParam("uname") String uname,@RequestParam("pwd") String pwd){
        System.out.println(uname+" "+pwd);

        return "search.html";
    }

    @RequestMapping("/login0")
    public String login(@RequestParam("uname") String uname,@RequestParam("pwd") String pwd,Model model){
        System.out.println(uname+" "+pwd);
        //判断是否拥有账号
        UserDaoImpl userDao = new UserDaoImpl();
        boolean login = userDao.login(uname, pwd);
        if(login){
            return "search.html";
        }
//        Session session = new Session();
//        session.setPersistent("User",);

        return "search.html";
    }

    @RequestMapping("/")
    public String index(){
        return "login.html";
    }


}
